<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class m_riwayat_pasien extends CI_Model {
	/* alergi */
	public function get_alergi($user_id){
		$this->db->where('KdPasien', $user_id);
		$this->db->order_by('KdAlergi', 'desc');
		$query = $this->db->get('alergi')->result_array();
		return $query;
	}
	public function insert_alergi($input, $user_id){
		for($i=0; $i<count($input['alergi']); $i++){
			$data = array(
				'KdPasien'		=> $user_id,
				'Alergi'			=> $input['alergi'][$i],
				'Keterangan'	=> $input['alergi_ket'][$i]
				);
			$query = $this->db->insert('alergi', $data);
		}
		if($query){
			set_flash('success','Data alergi berhasil ditambahkan');
			return true;
		}else{
			set_flash('success','Data alergi gagal ditambahkan');
			return true;
		}
	}
	public function update_alergi($KdPasien, $KdAlergi, $dataUpdate){
		$this->db->where('KdPasien', $KdPasien);
		$this->db->where('KdAlergi', $KdAlergi);
		$this->db->update('alergi', $dataUpdate);
		if($this->db->affected_rows() > 0){
			return true;
		}else{
			return false;
		}
	}
	public function destroy_alergi($id){
		$this->db->where("KdAlergi", $id);
		$query = $this->db->delete("alergi");
		
		if($query){
			set_flash('success','Data alergi berhasil dihapus');
			return true;
		}else{
			set_flash('success','Data alergi gagal dihapus');
			return true;
		}
	}

	/* penyakit section */
	public function get_r_penyakit($user_id){
		$this->db->where('KdPasien', $user_id);
		$this->db->order_by('KdRPenyakit', 'desc');
		$query = $this->db->get('r_penyakit')->result_array();
		return $query;
	}
	public function insert_r_penyakit($input, $user_id){
		for($i=0; $i<count($input['penyakit']); $i++){
			$data = array(
				'KdPasien'			=> $user_id,
				'Penyakit'			=> $input['penyakit'][$i],
				'Keterangan'		=> $input['keterangan'][$i]
				);
			$query = $this->db->insert('r_penyakit', $data);
		}
		if($query){
			set_flash('success','Data penyakit berhasil ditambahkan');
			return true;
		}else{
			set_flash('success','Data penyakit gagal ditambahkan');
			return true;
		}
	}
	public function update_penyakit($KdPasien, $KdRPenyakit, $dataUpdate){
		$this->db->where('KdPasien', $KdPasien);
		$this->db->where('KdRPenyakit', $KdRPenyakit);
		$this->db->update('r_penyakit', $dataUpdate);
		if($this->db->affected_rows() > 0){
			return true;
		}else{
			return false;
		}
	}
	public function destroy_penyakit($id){
		$this->db->where("KdRPenyakit", $id);
		$query = $this->db->delete("r_penyakit");
		
		if($query){
			set_flash('success','Data penyakit berhasil dihapus');
			return true;
		}else{
			set_flash('success','Data penyakit gagal dihapus');
			return true;
		}
	}

	public function get_r_obat($user_id){
		$this->db->join("obat", "obat.KdObat=r_obat.KdObat");
		$this->db->order_by('KdRObat', 'desc');
		$query = $this->db->get("r_obat")->result_array();
		return $query;
	}
	function get_obat() {
		$sql = 'SELECT * FROM obat';
		$query = $this->db->query($sql);
		if ($query->num_rows() > 0) {
			$result = $query->result_array();
			$query->free_result();
		} else {
			$result = array();
		}
		return $result;
	}
	public function insert_r_obat($input, $user_id){
		for($i=0; $i<count($input['obat']); $i++){
			$data = array(
				'KdPasien'		=> $user_id,
				'KdObat'			=> $input['obat'][$i],
				'Dosis'				=> $input['dosis'][$i],
				'Keterangan'	=> $input['keterangan'][$i]
				);
			$query = $this->db->insert('r_obat', $data);
		}
		if($query){
			set_flash('success','Data penyakit berhasil ditambahkan');
			return true;
		}else{
			set_flash('success','Data penyakit gagal ditambahkan');
			return true;
		}
	}
	public function update_r_obat($KdPasien, $KdRObat, $dataUpdate){
		$this->db->where('KdPasien', $KdPasien);
		$this->db->where('KdRObat', $KdRObat);
		$this->db->update('r_obat', $dataUpdate);
		if($this->db->affected_rows() > 0){
			return true;
		}else{
			return false;
		}
	}
	public function destroy_r_obat($id){
		$this->db->where("KdRObat", $id);
		$query = $this->db->delete("r_obat");
		
		if($query){
			set_flash('success','Data riwayat obat berhasil dihapus');
			return true;
		}else{
			set_flash('success','Data riwayat obat gagal dihapus');
			return true;
		}
	}

	/* riwayat keluarga */
	public function get_r_keluarga($user_id){
		$this->db->where('KdPasien', $user_id);
		$this->db->order_by('KdKeluarga', 'desc');
		$query = $this->db->get('r_keluarga')->result_array();
		return $query; 
	}
	public function insert_r_keluarga($input, $user_id){
		for($i=0; $i<count($input['keluarga']); $i++){
			$data = array(
				'KdPasien'			=> $user_id,
				'Keluarga'			=> $input['keluarga'][$i],
				'Keterangan'		=> $input['keterangan'][$i]
				);
			$query = $this->db->insert('r_keluarga', $data);
		}
		if($query){
			set_flash('success','Data riwayat keluarga berhasil ditambahkan');
			return true;
		}else{
			set_flash('success','Data riwayat keluarga gagal ditambahkan');
			return true;
		}
	}
	public function update_keluarga($KdPasien, $KdKeluarga, $dataUpdate){
		$this->db->where('KdPasien', $KdPasien);
		$this->db->where('KdKeluarga', $KdKeluarga);
		$this->db->update('r_keluarga', $dataUpdate);
		if($this->db->affected_rows() > 0){
			return true;
		}else{
			return false;
		}
	}
	public function destroy_keluarga($id){
		$this->db->where("KdKeluarga", $id);
		$query = $this->db->delete("r_keluarga");
		
		if($query){
			set_flash('success','Data keluarga berhasil dihapus');
			return true;
		}else{
			set_flash('success','Data keluarga gagal dihapus');
			return true;
		}
	}

	/* riwayat sosial */
	public function get_r_sosial($user_id){
		$this->db->where('KdPasien', $user_id);
		$this->db->order_by('KdRSosial', 'desc');
		$query = $this->db->get('r_sosial')->result_array();
		return $query;
	}
	public function insert_r_sosial($input, $user_id){
		for($i=0; $i<count($input['sosial']); $i++){
			$data = array(
				'KdPasien'			=> $user_id,
				'Sosial'			=> $input['sosial'][$i],
				'Keterangan'		=> $input['keterangan'][$i]
				);
			$query = $this->db->insert('r_sosial', $data);
		}
		if($query){
			set_flash('success','Data riwayat sosial berhasil ditambahkan');
			return true;
		}else{
			set_flash('success','Data riwayat sosial gagal ditambahkan');
			return true;
		}
	}
	public function update_sosial($KdPasien, $KdRSosial, $dataUpdate){
		$this->db->where('KdPasien', $KdPasien);
		$this->db->where('KdRSosial', $KdRSosial);
		$this->db->update('r_sosial', $dataUpdate);
		if($this->db->affected_rows() > 0){
			return true;
		}else{
			return false;
		}
	}
	public function destroy_sosial($id){
		$this->db->where("KdRSosial", $id);
		$query = $this->db->delete("r_sosial");
		
		if($query){
			set_flash('success','Data sosial berhasil dihapus');
			return true;
		}else{
			set_flash('success','Data sosial gagal dihapus');
			return true;
		}
	}

	/* riwayat kunjungan */
	public function get_r_kunjungan($user_id){
		$this->db->where('KdPasien', $user_id);
		$this->db->order_by('KdKunjungan', 'desc');
		$query = $this->db->get('kunjungan')->result_array();
		return $query;
	}
	public function update_kunjungan($KdPasien, $KdKunjungan, $dataUpdate){
		$this->db->where('KdPasien', $KdPasien);
		$this->db->where('KdKunjungan', $KdKunjungan);
		$this->db->update('kunjungan', $dataUpdate);
		if($this->db->affected_rows() > 0){
			return true;
		}else{
			return false;
		}
	}
	public function destroy_kunjungan($id){
		$this->db->where("KdKunjungan", $id);
		$query = $this->db->delete("kunjungan");
		
		if($query){
			set_flash('success','Data kunjungan berhasil dihapus');
			return true;
		}else{
			set_flash('success','Data kunjungan gagal dihapus');
			return true;
		}
	}

	/* hasil lab */
	public function get_hasillab($user_id){
		$this->db->join('kunjungan', 'kunjungan.KdKunjungan = r_hasil_lab.KdKunjungan');
		$this->db->where('KdPasien', $user_id);
		$query = $this->db->get('r_hasil_lab')->result_array();
		return $query;
	}
	public function insert_r_hasillab($input){
		if(isset($input['kk-hasillab'])){
			for($i=0; $i<count($input['kk-hasillab']); $i++){
				$data = array(
					'KdKunjungan'			=> $input['kk-hasillab'][$i],
					'Hasil'			=> $input['hasillab'][$i]
					);
				$query = $this->db->insert('r_hasil_lab', $data);
			}
		}else{
			$query = true;
		}

		if(isset($input['kk-resep'])){
			for($i=0; $i<count($input['kk-resep']); $i++){
				$data = array(
					'KdResep'	=> $input['kk-resep'][$i],
					'KdObat'				=> $input['obat'][$i]
					);
				$query = $this->db->insert('obat_resep', $data);
			}
			//print_r($input);
		}else{
			$query = true;
		}

		if($query){
			set_flash('success','Data riwayat hasil lab berhasil ditambahkan');
			return true;
		}else{
			set_flash('success','Data riwayat hasil lab gagal ditambahkan');
			return true;
		}
	}
	public function update_hasillab($KdHasilLab, $dataUpdate){
		$this->db->where('KdHasilLab', $KdHasilLab);
		$this->db->update('r_hasil_lab', $dataUpdate);
		if($this->db->affected_rows() > 0){
			return true;
		}else{
			return false;
		}
	}
	public function destroy_hasillab($id){
		$this->db->where("KdHasilLab", $id);
		$query = $this->db->delete("r_hasil_lab");
		
		if($query){
			set_flash('success','Data hasil lab berhasil dihapus');
			return true;
		}else{
			set_flash('success','Data hasil lab gagal dihapus');
			return true;
		}
	}
}

/* End of file m_riwayat_pasien.php */
/* Location: ./application/models/m_riwayat_pasien.php */